package cn.lhl.result;

import cn.lhl.action.util.ActionUtil;
import cn.lhl.mvc.ActionMapping;
import cn.lhl.mvc.ActionProxy;
import cn.lhl.mvc.ConfigurationManager;
import cn.lhl.mvc.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static cn.lhl.mvc.ActionProxy.*;

/**
 * Created by Administrator on 2017/4/1/001.
 */
public class ResultType {
    public static void ifResultType(String actionName,ConfigurationManager configurationManager){
        HttpServletRequest request=ServletActionContext.getRequest();
        HttpServletResponse response= ServletActionContext.getResponse();
        ActionMapping actionMapping = configurationManager.getActionMapper().getActionMapping(actionName);
        try {
            String action= getActionClass(actionMapping);
            if("chain".equals(actionMapping.getResult(action).getResultType())){
                ifResultType(actionMapping.getResult(action).getResultPath(),configurationManager);
            }else if("dispatcher".equals(actionMapping.getResult(action).getResultType())) {
                request.getRequestDispatcher(actionMapping.getResult(action).getResultPath()).forward(request, response);
            }else if("redirect".equals(actionMapping.getResult(action).getResultType())){
                response.sendRedirect(actionMapping.getResult(action).getResultPath());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
